INS
         NLS 
         TTL "DOS 3.3 CMD INTERPRETER"
         ORG $9D00
         OBJ $800
;
;
CH       EPZ $24
BASE     EPZ $28
CSW      EPZ $36
PROMPT   EPZ $33
KSW      EPZ $38
OPRND    EPZ $44
FILEBUF  EPZ $40
INTHIMEM EPZ $4C
INTLOMEM EPZ $4A
ASPGMST  EPZ $67
ASSTRS   EPZ $6F
ASVARS   EPZ $69
ASRUN    EPZ $76
ASHIMEM  EPZ $73
ASPEND   EPZ $AF
INTVEND  EPZ $CC
INTSTRT  EPZ $CA
PROTECT  EPZ $D6
ASONERR  EPZ $D8
INTRUN   EPZ $D9
IN       EQU $200
DOSRST   EQU $3D0
PWREDUP  EQU $3F4
SOFTEV   EQU $3F2
;
; RAM A/S ENTRY PTS
;
LC3C     EQU $C3C
LCF2     EQU $CF2
LE65     EQU $E65
LFD4     EQU $FD4
L1067    EQU $1067
;
;
;
;
;
DBUF     EQU $9CD4
FMGR2    EQU $AAFD
FMGR     EQU $AB06
;
; FILE MANAGER EQUATES
;
LB5A9    EQU $B5A9
VOLUME   EQU $B5BF
RECNUM   EQU $B5BD
NEWNAME  EQU RECNUM
FMSUBOP  EQU $B5BC
FMP      EQU $B5BB
FMOP     EQU $B5BB
RTNCODE  EQU $B5C5
FNADR    EQU $B5C3
DATARANG EQU $B5C3
FILETYPE EQU $B5C2
SLOT     EQU $B5C1
RANGELEN EQU $B5C1
DRIVE    EQU $B5C0
;
; DOS PATCHES
;
APNDPTCH EQU $B65E
APTCH2   EQU $B671
VFYPTCH  EQU $B686
APTCH3   EQU $B692
CALLRWTS EQU $B7B5
IOBDRV   EQU $B7EA
IOBSLOT  EQU $B7E9
AJUSTBYT EQU $BFDC
SETWARM  EQU $BFE6
ROM      EQU $C081
RAM      EQU $C080
;
;
;
; APPLESOFT EQUATES
;
LD43C    EQU $D43C
LD4F2    EQU $D4F2
LD665    EQU $D665
LD7D2    EQU $D7D2
LD865    EQU $D865
;
;
; INTEGER EQUATES
;
BASWRM   EQU $E003
BASCLD   EQU $E000
INTERR   EQU $E3E3
IRUN     EQU $E836
;
; MONITOR EQUATES
;
OLDBRK   EQU $FA59
INPRT    EQU $FE8B
OUTPRT   EQU $FE95
RTS      EQU $FF58
MON      EQU $FF65
;
;
;
;
DBUFP    ADR DBUF-1
DOSKBD   ADR KBDINTRC
DOSVID   ADR VIDINTRC
PFNADR   ADR FNAME
SFNADR   ADR SFNAME
LDRNGLEN ADR LOADLEN
LOADADR  ADR $9D00
FMPARMS  ADR FMOP
;
;
; CSW STATE HANDLER ADDRESSES
;
CSWSTADR ADR CSWST0-1
         ADR CSWST1-1
         ADR CSWST2-1
         ADR CSWST3-1
         ADR CSWST4-1
         ADR CSWST5-1
         ADR CSWST6-1
;
;
;COMMAND ADDRESSES
;
CMDTBL   ADR DOINIT-1
         ADR DOLOAD-1
         ADR DOSAVE-1
         ADR DORUN-1
         ADR DOCHAIN-1
         ADR DODELETE-1
         ADR DOLOCK-1
         ADR DOUNLOCK-1
         ADR DOCLOSE-1
         ADR DOREAD-1
         ADR DOEXEC-1
         ADR DOWRITE-1
         ADR DOPSTION-1
         ADR DOOPEN-1
         ADR DOAPND-1
         ADR DORENAME-1
         ADR DOCAT-1
         ADR DOMON-1
         ADR DONOMON-1
         ADR DOPRNUM-1
         ADR DOINNUM-1
         ADR DOMXFLS-1
         ADR DOFP-1
         ADR DOINT-1
         ADR DOBSAVE-1
         ADR DOBLOAD-1
         ADR DOBRUN-1
         ADR DOVERIFY-1
;
;
; ACTIVE BASIC TABLE
;
CHNADR   ADR IRUN
RUNADR   ADR DOSIRUN
BASERR   ADR INTERR
BASCOLD  ADR BASCLD
BASWARM  ADR BASWRM
ASFTREL  ADR LD4F2
;
; INTEGER STUFF
;
INTTBL   ADR IRUN
         ADR DOSIRUN
         ADR INTERR
         ADR BASCLD
         ADR BASWRM
;
; ROM APPLESOFT STUFF
;
FPTBL    ADR DOSARUN
         ADR DOSARUN
         ADR LD865
         ADR BASCLD
         ADR LD43C
         ADR LD4F2
;
; RAM APPLESOFT STUFF
;
RAMASTBL ADR DOSA2RUN
         ADR DOSA2RUN
         ADR L1067
         ADR DOSSTRT
         ADR LC3C
         ADR LCF2
;
;
;
;
; DOS COLDSTART ADDRESS
;
;
;
; INIT DEFAULT SLOT & DRIVE
;
DOSSTRT  LDA IOBSLOT
         LSR 
         LSR 
         LSR 
         LSR 
         STA SLOTVAL
         LDA IOBDRV
         STA DRVAL
;
;
; See which BASIC is active
;
         LDA BASCLD
         EOR #$20
         BNE LDFPBAS
;
;
; Integer BASIC is active,
; move all required addresses
; down there.
;
         STA WHCBASIC
         LDX #$A
LDINT    LDA INTTBL-1,X
         STA CHNADR-1,X
         DEX 
         BNE LDINT
         JMP CHSBASIC
;
;
; Applesoft BASIC is active,
; move A/S addresses to the
; active BASIC addresses area.
;
LDFPBAS  LDA #$40
         STA WHCBASIC
         LDX #$C
LDFPTBL  LDA FPTBL-1,X
         STA CHNADR-1,X
         DEX 
         BNE LDFPTBL
CHSBASIC SEC 
         BCS INITDOS
;
;
; DOS Warmstart
;
DOSWARM  LDA WHCBASIC
         BNE >1
;
; Cause integer BASIC to be
; selected.
;
         LDA #$20
         BNE >2
^1       ASL 
         BPL >3
;
;
; Select Applesoft BASIC.
;
         LDA #$4C
^2       JSR SETBASIC
^3       CLC 
;
;
;
; Initialize DOS.
;
INITDOS  PHP 
         JSR INITPTRS
         LDA #$0             ;set NOMON O,I,C
         STA MONFLGS
         STA CSWSTATE        ;init to state 0
         PLP 
         ROR 
         STA CURSTAT
         BMI >1
         JMP (BASWARM)
^1       JMP (BASCOLD)
;
;
;
; FRSTIME is call after a DOS coldstart
; it checks for RAM A/S and takes
; care of other coldstart stuff
; that must be done after BASIC
; is initialized.
;
FRSTIME  ASL                 ;check for RAM or ROM A/S
         BPL >2
;
;
; If RAM A/S, move required pointers
; into the active BASIC area.
;
         STA WHCBASIC
         LDX #$C
MOVRAMAS LDA RAMASTBL-1,X
         STA CHNADR-1,X
         DEX 
         BNE MOVRAMAS
         LDX #$1D
^1       LDA SFNAME,X
         STA FNAME,X
         DEX 
         BPL <1
;
;
; Init MAXFILES to the default
; value (usually 3)
;
^2       LDA MXFLS
         STA MAXFILES
;
; set up the DOS buffers chain
;
         JSR INITBUFS
;
;
; if EXEC is active, cause it to
; become inactive on the next
; character input
;
         LDA EXFLG
         BEQ >3
         PHA 
         JSR PNTEXEC
         PLA 
         LDY #0
         STA (FILEBUF),Y
;
;
; reset the CSW state to 0, set
; warmstart status.
;
^3       JSR RSET0
;
;
; check to see if we are in the
; middle of a command.
;
         LDA CMDINDX
         BNE TSTPEND
;
;
; If not, copy the page 3 stuff
; down to $3D0
;
         LDX #$2F
MOV3D0   LDA PAG3,X
         STA DOSRST,X
         DEX 
         BPL MOV3D0
         LDA PAG3+2
         STA SOFTEV+1
         EOR #$A5
         STA PWREDUP
         LDA PAG3+1
         STA SOFTEV
         LDA #$6
         BNE >1
;
;
; If there is a command pending
; (because RAM A/S was loaded?)
; then execute it, otherwise
; return to BASIC.
;
TSTPEND  LDA PENDCMD
         BEQ >2
^1       STA CMDINDX
         JMP DOCMD
^2       RTS 
;
;
; Stuff that gets moved down to
; $3D0
;
PAG3     JMP DOSWARM
         JMP DOSSTRT
         JMP FMGR2
         JMP CALLRWTS
         LDA FMPARMS+1
         LDY FMPARMS
         RTS 
         LDA LAAC1+1
         LDY LAAC1
         RTS 
         JMP INITPTRS
         NOP 
         NOP 
         JMP OLDBRK
         JMP MON
         JMP RTS
         JMP MON
         JMP MON
         ADR $FF65
;
;
; Keyboard intercept routine.
; Called each time a character
; is requested.
; "HOOKS" at $38 and $39 point
; at this routine.
;
KBDINTRC JSR SAVREG
         LDA CURSTAT         ;reading from a file?
         BEQ >2
;
; If so, remove cursor from screen
;
         PHA 
         LDA ASAVE
         STA (BASE),Y
         PLA 
         BMI >1              ;doing coldstart?
         JMP READBYTE
;
;
; special case for coldstart
;
^1       JSR FRSTIME
         LDY CH
         LDA #$60
         STA (BASE),Y
^2       LDA EXFLG
         BEQ >1
         JSR EXECRD
^1       LDA #3
         STA CSWSTATE
         JSR REGRST
         JSR RDCHAR
         STA ASAVE
         STX XSAVE
         JMP DOSXIT
RDCHAR   JMP (KSW)
;
;
; Video input state handler.
; Transfers control to the
; proper output handler.
;
VIDINTRC JSR SAVREG
         LDA CSWSTATE
         ASL 
         TAX 
         LDA CSWSTADR+1,X
         PHA 
         LDA CSWSTADR,X
         PHA 
         LDA ASAVE
         RTS                 ;Funny JMP (--)
;
;
; Register save routine
;
SAVREG   STA ASAVE
         STX XSAVE
         STY YSAVE
         TSX 
         INX 
         INX 
         STX SSAVE
         LDX #3
^1       LDA CSWL,X
         STA CSW,X
         DEX 
         BPL <1
         RTS 
;
;
; CSW STATE 0: Start of line.
; Check for control-D at
; beginning of line.
;
; If RUN command was delayed while
; loading A/S, continue RUN cmd.
;
CSWST0   LDX RUNINTRC
         BEQ >1
         JMP RUNDONE
;
; Check to see if a file is
; being read.
;
^1       LDX CURSTAT
         BEQ >1
;
; If a file is being read, check
; to see if "?" prompt was output
;
         CMP #"?"            ;A/S PROMPT
         BEQ CSWST6
;
;
; See if at beginning of line
;
         CMP PROMPT
         BEQ CSWST2.1
;
; If not control-D at beginning
; of line, goto state 2,
;
; otherwise go to state one and
; collect possible DOS command.
;
^1       LDX #2
         STX CSWSTATE
         CMP CTLD
         BNE CSWST2
         DEX 
         STX CSWSTATE
         DEX 
         STX CMDLNIDX
;
; Collect DOS command from
; input line and parse
;
CSWST1   LDX CMDLNIDX
C1LP     STA IN,X
         INX 
         STX CMDLNIDX
         CMP #$8D
         BNE ECHOC
         JMP PARSE
;
;
;
; CSW STATE 2 handler, Non-DOS
; command.
;
; If current input character isn't
; a return, exit DOS through ECHO,
; otherwise reset command state
; to zero.
;
CSWST2   CMP #$8D
         BNE ECHO
CSWST2.1 LDX #0
         STX CSWSTATE
         JMP ECHO
;
;
; STATE 3 handler- INPUT statement
; handler.
;
;
; set the command state to zero
; as a default (in case input
; ends).
;
CSWST3   LDX #0
         STX CSWSTATE
         CMP #$8D
         BEQ >1
;
; If not at end of input, check
; the exec flag.
; If set, echo character only if
; MON I is set or exec is active
; In either case the state will be
; reset to three.
;
;
C3CHKEXC LDA EXFLG
         BEQ ECHO
         BNE ECHOI
;
;
;  Come here if EOLN encountered.
; If BASIC is not running, or
; EXECing a file goto state one
; and check for a possible DOS
; command.  Otherwise exit DOS
; echoing character as appropriate
; after switching to state 1
;
;
;
^1       PHA 
         SEC 
         LDA EXFLG
         BNE >1
         JSR ISBASRUN
^1       PLA 
         BCC C3CHKEXC
         LDX XSAVE
         JMP C1LP
;
;
; STATE 4 handler.
; Write data to a file.
;
; check for EOLN, switch to
; state 5 if <cr> encountered.
;
CSWST4   CMP #$8D
         BNE >1
         LDA #5
         STA CSWSTATE
^1       JSR WRTBYTE
         JMP ECHOO
;
;
; STATE 5 handler, start of
; write data line; check for CTL-D
;
CSWST5   CMP CTLD
         BEQ CSWST0
;
; treat any linefeeds as a
; continuation of the previous
; line.
;
         CMP #$8A
         BEQ <1
;
; Otherwise revert to state 4
;
         LDX #4
         STX CSWSTATE
         BNE CSWST4
;
; STATE 6 handler, skip output of
; prompt character.
;
CSWST6   LDA #0
         STA CSWSTATE
         BEQ ECHOI
;
;
;
; Finish RUN command interrupted
; by RAM A/S load.
;
RUNDONE  LDA #0
         STA RUNINTRC
         JSR INITPTRS
         JMP DORUN.1
;
;
; End of DOS cmd scan.
; Remove DOS command and pretend
; the user only pressed return
;
SCNXIT   LDA IN
         CMP CTLD
         BEQ ECHOC
         LDA #$8D
         STA IN
         LDX #0
         STX XSAVE
;
;
; Echo character conditionally to
; the screen:
;
;  ECHOC - IF MON C
;  ECHOO - IF MON O
;  ECHOI - IF MON I
;
;  ECHO  - UNCONDITIONALLY
;
ECHOC    LDA #$40
         BNE >1
ECHOO    LDA #$10
         BNE >1
ECHOI    LDA #$20
^1       AND MONFLGS
         BEQ DOSXIT
ECHO     JSR REGRST
         JSR COUT
         STA ASAVE
         STY YSAVE
         STX XSAVE
;
;
; Restore registers and exit DOS
;
DOSXIT   JSR INITPTRS
         LDX SSAVE
         TXS 
REGRST   LDA ASAVE
         LDY YSAVE
         LDX XSAVE
         SEC 
         RTS 
;
;
; COUT and CRLF routines
;
COUT     JMP (CSW)
CRLF     LDA #$8D
         JMP COUT
;
;
; Parse DOS command and check
; syntax
;
PARSE    LDY #$FF
         STY CMDINDX         ;set to 0 (see ADDCHR)
         INY 
         STY PENDCMD
;
; get successive characters from
; input buffer and compare against
; current command in table
;
ADDCHR   INC CMDINDX
         LDX #0
         PHP                 ;assume "="
         LDA IN,X            ;skip any control-D's
         CMP CTLD
         BNE >1
         INX 
^1       STX CMDLNIDX
^2       JSR FLSHCMDL        ;skip blanks
         AND #$7F
         EOR DOSCMDS,Y       ;compare to char in table
         INY 
;
; the following code is a tricky
; way of determining if the entire
; string is equal to one of the
; DOS commands
;
         ASL 
         BEQ >1
         PLA 
         PHP 
^1       BCC <2
         PLP 
         BEQ CMPUTIDX
         LDA DOSCMDS,Y
         BNE ADDCHR          ;at last command?
;
; If command was not found, see if
; first character on the line was
; control-D, if so SYNTAX ERROR!!!
;
GETFRST  LDA IN
         CMP CTLD
         BEQ >1
         JMP ECHO
^1       LDA IN+1
         CMP #$8D
         BNE >1
         JSR RSET0
         JMP ECHOC
^1       JMP CSYNERR
;
;
; Valid DOS command, get its
; address and execute it.
;
CMPUTIDX ASL CMDINDX
         LDY CMDINDX
         JSR ISBASRUN
         BCC >1
;
; Certain commands (such as OPEN,
; etc.) can only be executed if
; BASIC is running.  If such a
; command is executed and BASIC
; is not running, fall through
; to here.
;
         LDA #2
         AND KWRDPRMS,Y
         BEQ >1
;
; NOT A DIRECT COMMAND
;
         LDA #$F
         JMP DOERROR
;
;
^1       CPY #6
         BNE >1
         STY PROMPT
;
;
; See if a filename is required
;
^1       LDA #$20
         AND KWRDPRMS,Y
         BEQ FNDNONAM
;
; If a filename is needed, blank
; out the filename arrays and
; collect a filename (if found)
;
         JSR BLKNAMES
         PHP 
;
; skip any leading blanks
;
CI.1     JSR FLSHCMDL
         BEQ >2
         ASL 
         BCC >1
         BMI >1
         JMP GETFRST
^1       ROR 
         JMP >1
CLRNAME  JSR GNXTCHR
         BEQ >2
^1       STA FNAME,Y
         INY 
         CPY #$3C
         BCC CLRNAME
^1       JSR GNXTCHR
         BNE <1
^2       PLP 
         BNE >1
         LDY CMDINDX
         LDA #$10
         AND KWRDPRMS,Y
         BEQ >2
         LDY #$1E
         PHP 
         BNE CI.1
^1       LDA SFNAME
         CMP #$A0
         BEQ CHKFRST
^2       LDA FNAME
         CMP #$A0
         BNE SETDFLTS
         LDY CMDINDX
         LDA #$C0
         AND KWRDPRMS,Y
         BEQ CHKFRST
         BPL SETDFLTS
CHKFRST  JMP GETFRST
;
;
; subroutine to blank out the
; filenames.
;
BLKNAMES LDY #$3C
BLKN2    LDA #$A0
^1       STA MONVAL,Y
         DEY 
         BNE <1
         RTS 
;
;
; Branch here if no filename
; is required.
;
FNDNONAM STA FNAME
;
; check parameter table to see
; if a positional parameter is
; required.
;
         LDA #$C
         AND KWRDPRMS,Y
         BEQ SETDFLTS
;
; If so, read in the number
;
         JSR GETNUM
;
; was number ommitted?
;
         BCS FNN.2
         TAY 
         BNE FNN.1
;
; check the range of the number
;
         CPX #$11
         BCS FNN.1
         LDY CMDINDX
         LDA #8
         AND KWRDPRMS,Y
         BEQ >1
         CPX #8
         BCS CHKFRST
;
; if # within range, set defaults
;
         BCC SETDFLTS
^1       TXA 
         BNE SETDFLTS
FNN.1    LDA #2
         JMP DOERROR
FNN.2    JMP CSYNERR
;
;
; Set up defaults for the
; positional values
;
SETDFLTS LDA #0
         STA KYWRDFND
         STA MONVAL
         STA VOLVAL
         STA LENVAL
         STA LENVAL+1
         JSR AJUSTBYT
         LDA CMDLNIDX
;
; Come here if positionals are
; required.
;
;
; First, skip any leading  blanks
;
GETNXT   JSR FLSHCMDL
         BNE >1
         CMP #$8D
         BNE GETNXT
;
; If at EOLN, make sure we
; have all the arguements
; we need.
;
         LDX CMDINDX
         LDA KYWRDFND
         ORA KWRDPRMS+1,X
         EOR KWRDPRMS+1,X
         BNE CHKFRST
         LDX TEMP1
         BEQ PROCMD
         STA TEMP1
         STX CMDLNIDX
         BNE GETNXT
^1       LDX #$A
^2       CMP PPARMS-1,X
         BEQ >3
         DEX 
         BNE <2
ISLE     BEQ FNN.2
;
; As each positional arguement
; is encountered, make it as
; present in KYWRDFND
;
^3       LDA PARMBITS-1,X
         BMI UPDATMON
         ORA KYWRDFND
         STA KYWRDFND
         DEX 
         STX KYWRDIDX
;
; get numeric value associated
; with the keyword.
;
         JSR GETNUM
         BCS FNN.2
;
; check the range of the value
; following the keyword.
;
         LDA KYWRDIDX
         ASL 
         ASL 
         TAY 
         LDA OPRND+1
         BNE >4
         LDA OPRND
         CMP KWRANGE,Y
         BCC FNN.1
         LDA OPRND+1
^4       CMP KWRANGE+3,Y
         BCC >6
^5       BNE FNN.1
         LDA OPRND
         CMP KWRANGE+2,Y
         BCC >6
         BNE <5
^6       LDA TEMP1
         BNE GETNXT
;
; save value parsed in the keyword
; parameters table.
;
         TYA 
         LSR 
         TAY 
         LDA OPRND+1
         STA VOLVAL+1,Y
         LDA OPRND
         STA VOLVAL,Y
GNISLE   JMP GETNXT
;
;
; If O,I, or C was encountered in
; MON or NOMON, set appropriate 
; bits.
;
UPDATMON PHA 
         LDA #$80
         ORA KYWRDFND
         STA KYWRDFND
         PLA 
         AND #$7F
         ORA MONVAL
         STA MONVAL
         BNE GNISLE
         BEQ ISLE
;
;
;
; Process valid DOS command
;
PROCMD   JSR DOCMD
         JMP SCNXIT
DOCMD    JSR RSET0
         JSR CLRFMP
         LDA CMDINDX
         TAX 
         LDA CMDTBL+1,X
         PHA 
         LDA CMDTBL,X
         PHA 
         RTS 
;
;
; Get next character from command
; line and check for a <cr> or
; a comma.
;
GNXTCHR  LDX CMDLNIDX
         LDA IN,X
         CMP #$8D
         BEQ >7
         INX 
         STX CMDLNIDX
         CMP #","
^7       RTS 
;
;
; FLSHCMDL- deletes leading blanks
; from command line.  Z-flag is
; returned set if "," or <cr> is
; first non-blank encountered.
;
;
FLSHCMDL JSR GNXTCHR
         BEQ <7
         CMP #$A0
         BEQ FLSHCMDL
         RTS 
;
;
; CLRFMP- clears file manager's
; parameter area.
;
CLRFMP   LDA #0
         LDY #$16
^8       STA FMOP-1,Y
         DEY 
         BNE <8
         RTS 
;
;
; GETNUM- converts numeric (in
; ASCII) arguement to binary.
;
; DECIMAL and HEX are supported.
;
GETNUM   LDA #0
         STA OPRND
         STA OPRND+1
         JSR FLSHCMDL
         PHP 
         CMP #"$"
         BEQ HEXCONV
         PLP 
         JMP GN.2
;
; Decimal input routine here
;
GN.1     JSR FLSHCMDL
GN.2     BNE >9
         LDX OPRND
         LDA OPRND+1
         CLC 
         RTS 
;
; get current character, multiply
; OPRND by 10, and add in numeric
; value for current digit
;
^9       SEC 
         SBC #"0"
         BMI BADNUM
         CMP #$A
         BCS BADNUM
;
; multiply OPRND by 10
; (OPRND*2 + OPRND*8)
;
; and add in digit value
;
;
         JSR MUL2
         ADC OPRND
         TAX 
         LDA #0
         ADC OPRND+1
         TAY 
         JSR MUL2
         JSR MUL2
         TXA 
         ADC OPRND
         STA OPRND
         TYA 
         ADC OPRND+1
         STA OPRND+1
         BCC GN.1
BADNUM   SEC 
         RTS 
;
; multiply OPRND by 2.
;
MUL2     ASL OPRND
         ROL OPRND+1
         RTS 
;
;
; HEX number here.
;
HEXCONV  PLP 
HEXLOOP  JSR FLSHCMDL
         BEQ GN.2
         SEC 
         SBC #$B0
         BMI BADNUM
         CMP #$A
         BCC >1
         SBC #7
         BMI BADNUM
         CMP #$10
         BCS BADNUM
^1       LDX #4
^2       JSR MUL2
         DEX 
         BNE <2
         ORA OPRND
         STA OPRND
         JMP HEXLOOP
;
;
; Do PR#n command
;
DOPRNUM  LDA OPRND
         JMP OUTPRT
;
;
; Do IN# command
;
DOINNUM  LDA OPRND
         JMP INPRT
;
;
; Do MON and NOMON commands
;
DOMON    LDA MONFLGS
         ORA MONVAL
         STA MONFLGS
         RTS 
;
DONOMON  BIT MONVAL
         BVC >3
         JSR CRLF
^3       LDA #$70
         EOR MONVAL
         AND MONFLGS
         STA MONFLGS
         RTS 
;
;
; Handle MAXFILES commmand
;
DOMXFLS  LDA #0
         STA EXFLG
         LDA OPRND
         PHA 
         JSR CLOSEALL
         PLA 
         STA MAXFILES
         JMP INITBUFS
;
;
; Handle DOS DELETE command
;
DODELETE LDA #5
         JSR CMDHNDL2        ;perform the delete.
;
; Free up the buffer used by
; the delete command.
;
         JSR LOCBUF
         LDY #0
         TYA 
         STA (FILEBUF),Y
         RTS 
;
;
; Handle LOCK and UNLOCK
;
DOLOCK   LDA #7
         BNE >4
DOUNLOCK LDA #8
^4       JSR CMDHNDL2
         JMP DOCLOSE
;
;
; Handle DOS verify command
;
DOVERIFY LDA #$C
         BNE <4
;
;
; Handle DOS RENAME command
;
DORENAME LDA SFNADR
         STA NEWNAME
         LDA SFNADR+1
         STA NEWNAME+1
         LDA #9
         STA TEMP1
         JSR CLOSOPN
         JMP DOCLOSE
;
;
; APPEND command is handled here.
; Just do an open and then read
; the file until a $00 is
; encountered.
;
DOAPND   JSR DOOPEN
^5       JSR RDTEXT
         BNE <5
         JMP APTCH2
;
;
; OPEN a text file here
;
DOOPEN   LDA #0
         JMP OPENTST
;
;
; File manager setup used by the
; various DOS commands
;
CMDHNDLR LDA #1
CMDHNDL2 STA TEMP1
         LDA LENVAL
         BNE >6
         LDA LENVAL+1
         BNE >6
         LDA #1
         STA LENVAL
^6       LDA LENVAL
         STA RECNUM
         LDA LENVAL+1
         STA RECNUM+1
;
; Close a file if already open.
;
CLOSOPN  JSR DOCLOSE
         LDA OPRND+1
         BNE >7
         JMP NOBUF
^7       STA FILEBUF+1
         LDA OPRND
         STA FILEBUF
         JSR COPYFNAM
         JSR COPYPTRS
         JSR COPYPARM
         LDA TEMP1
         STA FMOP
         JMP FMDRVR
;
;
; DOS CLOSE command
;
DOCLOSE  LDA FNAME
         CMP #$A0
         BEQ CLOSEALL
         JSR LOCBUF
         BCS RTS0
         JSR CLOSFREE
         JMP DOCLOSE
;
;
; FREE any buffers used by file
; being closed.
;
CLOSFREE JSR ISEXCBUF
         BNE >8
         LDA #0
         STA EXFLG
^8       LDY #0
         TYA 
         STA (FILEBUF),Y
         JSR COPYPTRS
         LDA #2
         STA FMOP
         JMP FMDRVR
;
;
; Close all open files
;
CLOSEALL JSR FRSTBUF
         BNE >0
^9       JSR PNTNXTBF
         BEQ RTS0
^0       JSR ISEXCBUF
         BEQ <9
         JSR GFBFNAM
         BEQ <9
         JSR CLOSFREE
         JMP CLOSEALL
RTS0     RTS 
;
;
; DOS BSAVE COMMAND
;
DOBSAVE  LDA #9              ;check parameters
         AND KYWRDFND
         CMP #9
         BEQ >1
         JMP GETFRST
;
; Open file and make sure it's
; Binary.
;
^1       LDA #4
         JSR OPENTST
;
; Init address parameters
;
         LDA ADRVAL+1
         LDY ADRVAL
         JSR WRT2
         LDA LENVAL+1
         LDY LENVAL
         JSR WRT2
         LDA ADRVAL+1
         LDY ADRVAL
         JMP RWRANGE
;
;
; DOS BLOAD COMMAND
;
DOBLOAD  JSR CMDHNDLR
;
; Make sure it is a binary file
;
         LDA #$7F
         AND FILETYPE
         CMP #4
         BEQ >2
         JMP FMISMTCH
^2       LDA #4
         JSR OPENTST
;
; Read in length and address
; values.
;
         JSR RD2
         TAX 
         LDA KYWRDFND
         AND #1
         BNE >2
         STX ADRVAL
         STY ADRVAL+1
^2       JSR RD2
         LDX ADRVAL
         LDY ADRVAL+1
         JMP RWSETUP
;
;
; Handle BRUN command
;
DOBRUN   JSR DOBLOAD
         JSR INITPTRS
         JMP (ADRVAL)
;
;
; Handle DOS SAVE command 
;
DOSAVE   LDA WHCBASIC
         BEQ ISINT
;
; Applesoft BASIC at this point
;
;
; Is source protected?
;
         LDA PROTECT
         BPL >3
         JMP PTOOBIG
^3       LDA #2
         JSR OPENTST
         SEC 
         LDA ASPEND
         SBC ASPGMST
         TAY 
         LDA ASPEND+1
         SBC ASPGMST+1
         JSR WRT2
         LDA ASPGMST+1
         LDY ASPGMST
         JMP RWRANGE
;
; Saving integer BASIC
;
ISINT    LDA #1
         JSR OPENTST
         SEC 
         LDA INTHIMEM
         SBC INTSTRT
         TAY 
         LDA INTHIMEM+1
         SBC INTSTRT+1
         JSR WRT2
         LDA INTSTRT+1
         LDY INTSTRT
         JMP RWRANGE
;
; Open a file and check its type
;
OPENTST  STA FILETYPE
         PHA 
         JSR CMDHNDLR
         PLA 
         JMP CHKTYPE
;
; Write two bytes out to a file
;
WRT2     STY RANGELEN
         STY DATARANG
         STA RANGELEN+1
         LDA #4
         STA FMOP
         LDA #1
         STA FMSUBOP
         JSR FMDRVR
         LDA RANGELEN+1
         STA DATARANG
         JMP FMDRVR
;
;
; Read or Write a range of bytes
;
RWRANGE  STY DATARANG
         STA DATARANG+1
         LDA #2
         JMP VFYPTCH
RWR.2    JSR FMDRVR
         JMP DOCLOSE
FTMIS    JMP FMISMTCH
;
;
; Do BASIC LOAD
;
DOLOAD   JSR CLOSEALL
DOLD.2   JSR CMDHNDLR
         LDA #$23            ;is type INT, RAM A/S or ROM A/S?
         AND FILETYPE
         BEQ FTMIS
         STA FILETYPE
         LDA WHCBASIC
         BEQ >7
;
; select Applesoft
;
         LDA #2
         JSR SELBASIC
;
;
; Read in length of A/S program
;
         JSR RD2
;
;
; compute A/S pointer values
;
         CLC 
         ADC ASPGMST
         TAX 
         TYA 
         ADC ASPGMST+1
         CMP ASHIMEM+1
         BCS PTOOLRG
         STA ASPEND+1
         STA ASVARS+1
         STX ASPEND
         STX ASVARS
         LDX ASPGMST
         LDY ASPGMST+1
         JSR RWSETUP
         JSR INITPTRS
         JMP (ASFTREL)
;
; select integer BASIC and load
; integer program
;
^7       LDA #1
         JSR SELBASIC
         JSR RD2
         SEC 
         LDA INTHIMEM
         SBC LOADLEN
         TAX 
         LDA INTHIMEM+1
         SBC LOADLEN+1
         BCC PTOOLRG
         TAY 
         CPY INTLOMEM+1
         BCC PTOOLRG
         BEQ PTOOLRG
         STY INTSTRT+1
         STX INTSTRT
;
;
; Read or Write the range of bytes
; required.
;
RWSETUP  STX DATARANG
         STY DATARANG+1
         JMP RWR.2
;
;
; Read two bytes from the
; specified file.
;
RD2      LDA LDRNGLEN
         STA DATARANG
         LDA LDRNGLEN+1
         STA DATARANG+1
         LDA #0
         STA RANGELEN+1
         LDA #2
         STA RANGELEN
         LDA #3
         STA FMOP
         LDA #2
         STA FMSUBOP
         JSR FMDRVR
         LDA LOADLEN+1
         STA RANGELEN+1
         TAY 
         LDA LOADLEN
         STA RANGELEN
         RTS 
;
; Print "PROGRAM TOO LARGE"
;
PTOOLRG  JSR DOCLOSE
         JMP PTOOBIG
;
; Select a BASIC.
; If BASIC matches filetype, quit.
; If not, select alternate BASIC.
;
;
; See if the proper BASIC is
; already selected.
;
SELBASIC CMP FILETYPE
         BEQ RTS1
;
; If not, save CMDINDX and
; change BASICs
;
         LDX CMDINDX
         STX PENDCMD
         LSR 
         BEQ >9
         JMP DOINT
;
; If changing to Applesoft,
; save filename in case RAM
; A/S is loading (remember,
; RAM A/S is an integer program
; called "APPLESOFT", during
; its load it will wipe out
; FNAME).
;
^9       LDX #$1D
^0       LDA FNAME,X
         STA SFNAME,X
         DEX 
         BPL <0
         JMP DOFP
RTS1     RTS 
         ICL "CDI.PART2"

